package com.bst.bsj.bops.web.biz.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bst.bsj.bops.BopsConstants;
import com.bst.bsj.bops.web.biz.SystemService;
import com.bst.bsj.common.dal.daointerface.MerchantDAO;
import com.bst.bsj.common.dal.dataobject.Merchant;
import com.homewaimai.common.lang.MD5Util;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.ResultSupport;
import com.homewaimai.common.lang.StringUtil;

@Service("systemService")
public class SystemServiceImpl implements SystemService {

    private static final Log logger = LogFactory.getLog(SystemServiceImpl.class);

    @Autowired
    private MerchantDAO      merchantDAO;

    @Override
    public Result loginMerchant(String identity, String passwd, int type) {
        Result rst = new ResultSupport(false);
        if (StringUtil.isEmpty(identity) || StringUtil.isEmpty(passwd)) {
            rst.setSuccess(false);
            rst.getModels().put("error", "参数不正确！");
            return rst;
        }
        String pwdmd5 = MD5Util.getMD5String(passwd);
        if (type == BopsConstants.LOGIN_TYPE_MOBILE) {
            return loginMerchantByMobile(identity, pwdmd5);
        }
        rst.getModels().put("error", "登录类型不可用！");
        return rst;
    }

    /**
     * 手机 ＋ 密码登录
     * 
     * @param mobile
     * @param pwd
     * @return
     */
    private Result loginMerchantByMobile(String mobile, String pwd) {
        Merchant merchant = merchantDAO.selectByMobile(mobile);
        Result rst = new ResultSupport(false);
        if (merchant == null || !merchant.getPassword().equals(pwd)) {
            rst.setSuccess(false);
            rst.getModels().put("error", "手机或者密码错误！");
            return rst;
        }
        rst.setSuccess(true);
        rst.getModels().put("data", merchant);
        return rst;
    }

}
